package com.wgchao.identityrightsmanagement.domain.resource.model;

import com.wgchao.identityrightsmanagement.domain.IdentifiedDomainObject;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectDisabledException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @ClassName: ProjectResources
 * @author: wgchao
 * @createTime: 2020/6/9 3:44 PM
 * @Description: 项目资源
 */
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ProjectResources extends IdentifiedDomainObject {
    //标识
    private String proMark;

    private String proName;

    //描述/备注
    private String description;

    //禁用标志
    private boolean disabled;

    public ProjectResources(Long id, String proMark, String proName, String description, boolean disabled, Long version) {
        setId(id);
        this.proMark = proMark;
        this.proName = proName;
        this.description = description;
        this.disabled = disabled;
        setVersion(version);
    }

    /**
     * 添加项目资源
     * @param creator
     * @return
     */
    public static ProjectResources addProject(ProjectResourcesCreator creator){
        return new ProjectResources(creator.getProMark(), creator.getProName(), creator.getDescription(), false);
    }

    /**
     * 修改项目资源基本信息
     * @param updator
     */
    public void modifyProjectInfo(ProjectResourcesUpdator updator){
        if(!normal()){
            throw new ObjectDisabledException("项目资源不可用");
        }
        if(updator.getProName() != null){
            this.proName = updator.getProName().orElse(null);
        }
        if(updator.getDescription() != null){
            this.description = updator.getDescription().orElse(null);
        }
    }

    public boolean normal(){
        return !this.disabled;
    }

    public void normalException(){
        if(!this.normal()){
            throw new ObjectDisabledException(this.proName+"项目不可用");
        }
    }

    //禁用
    public void disable(){
        this.disabled = true;
    }

    //恢复使用
    public void regain(){
        this.disabled = false;
    }

}
